using System.Collections.Generic;

namespace abilities.parameters
{
    /// <summary>
    /// 属性数值集合，用来描述英雄有哪些属性，并可以监听这些属性的变化
    /// </summary>
    public class ParameterCollection:Dictionary<ParameterEntry, ParameterValue>
    {
        public ParameterChangeSignal ParamChangedSignal = new ();

        public ParameterValue GetParameter(ParameterEntry entry ) {
            if (this.TryGetValue(entry, out var it))
                return it;
            return ParameterValue.ZERO;
        }

        public int GetBase(ParameterEntry entry ){
            if(this.TryGetValue(entry,out var it))
                return it.Base;
            return 0;
        }

        public int GetCurrent(ParameterEntry entry ){
            if(this.TryGetValue(entry,out var it))
                return it.Current;
            return 0;
        }

        public bool Has(ParameterEntry entry)
        {
            return this.ContainsKey(entry);
        }

        public bool IsEmpty(){
            return this.Count < 1;
        }

        public void AddCollection(ParameterCollection parameterCollection)
        {
            foreach (var it in parameterCollection)
            {
                Add(it.Key,new ParameterValue(it.Value));
            }
        }

        public int SetBase(ParameterEntry entry , int value) 
        {
            if (TryGetValue(entry, out var it))
            {
                it.Base = value;
            }
            else
            {
                Add(entry, it = new ParameterValue(value));
            }
            ParamChangedSignal.Dispatch(entry,it);
            return it.Base;
        }

        public int SetCurrent(ParameterEntry entry , int value) {
            if (TryGetValue(entry, out var it))
            {
                it.Current = value;
            }
            else
            {
                Add(entry, it = new ParameterValue(value,value));
            }
            ParamChangedSignal.Dispatch(entry,it);
            return it.Current;
        }

        public int DeltaBase(ParameterEntry entry , int delta) {
            if (delta == 0)
            {
                return GetBase(entry);
            }

            if (TryGetValue(entry,out var it))
            {
                it.Base += delta;
            }
            else
            {
                Add(entry, it = new ParameterValue(delta));
            }

            ParamChangedSignal.Dispatch(entry,it);
            return it.Base;
        }

        public int DeltaCurrent(ParameterEntry entry , int delta) {
            if (delta == 0)
            {
                return GetCurrent(entry);
            }

            if (TryGetValue(entry,out var it))
            {
                it.Current += delta;
            }
            else
            {
                Add(entry, it = new ParameterValue(0, delta));
            }

            ParamChangedSignal.Dispatch( entry,it);
            return it.Current;
        }
    }
}